/*
 ------------------------------------------------------------------------------
	sdlmad - a test program for using mad library with SDL on Win32
	and Linux.

	Copyright (C) 2011 Pierre Veber
	
	This program is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation; either version 3 of the License, or
	(at your option) any later version.
	
	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.
	
	You should have received a copy of the GNU General Public License
	along with this program; if not, write to the Free Software Foundation,
	Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
------------------------------------------------------------------------------
*/

#include "stdafx.h"
#include "sdlmad.h"

audio_data_t audio;

void cleanup(void)
{
	waveClose(&audio);
	SDL_Quit();
}

int main(int argc, char* argv[])
{
	SDL_Surface* pScreen;
	SDL_Event event;
	decode_thread_data_t* pDecode = NULL;

	if ( waveInit(&audio) != 0)
	{
		TRACE("Unable to initialize audio !\n");
		exit(1);
	}

	if (SDL_Init(SDL_INIT_VIDEO) < 0)
	{
		TRACE("Could not initialize SDL: %s\n", SDL_GetError());
		exit(1);
	}
	atexit(cleanup);
	
	pScreen = SDL_SetVideoMode(320, 320, 0, SDL_HWPALETTE);
	if (pScreen == NULL)
	{
		TRACE("Couldn't set screen mode: %s\n", SDL_GetError());
		exit(1);
	}
		
	SDL_WM_SetCaption("willy-player/PROOF/4", NULL);

	while(SDL_WaitEvent(&event))
	{
		if (event.type == SDL_QUIT)
		{
			exit(0);
		}
		else if (event.type == SDL_KEYDOWN)
		{
			switch (event.key.keysym.sym)
			{
				case SDLK_ESCAPE:
					exit(0);
					break;
			
				case SDLK_SPACE:
					if ( pDecode == NULL )
					{
						pDecode = madDecode(&audio, argv[1]);
					}
					else
					{
						madStop(pDecode);
						pDecode = NULL;
					}
					break;

				default:
					break;
			}
		}
	}
	return 0;
}

#ifdef _MSC_VER
static char* _argv[] = { "sdlmad.exe", "" };
int APIENTRY _tWinMain(
			HINSTANCE hInstance,
			HINSTANCE hPrevInstance,
			LPTSTR    lpCmdLine,
			int       nCmdShow
			)
{
	/* convert command line to UTF-8 */
	char* pszCmdLine = (char*)malloc(_tcslen(lpCmdLine)+1);
	WideCharToMultiByte(CP_UTF8, 0, lpCmdLine, -1 ,pszCmdLine, _tcslen(lpCmdLine)+1, NULL, NULL);
	_argv[1] = pszCmdLine;
	return main(0, _argv);
}
#endif
